package com.ruoyi.app.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.app.common.constant.UploadConstants;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.NumberUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.Md5Utils;
import okhttp3.*;

import java.io.IOException;

public class ApiSpaceUtils {

    private static final String TOKEN = "49wcwjnsorm7ogqs05mna791g2999dlq";
    private static final String QR_CODE_URL = "https://eolink.o.apispace.com/ercode/api/v1/forward/qr_code";
    private static final String SMS_NOTIFY_URL = "https://eolink.o.apispace.com/sms-notify/notify";
    private static OkHttpClient client;

    static {
        client = new OkHttpClient().newBuilder().build();
    }

    public static String createUrlQrCode(String url) {
        String fileName = "/" + DateUtils.datePath() + "/" + Md5Utils.hash(DateUtils.dateTimeNow() + NumberUtils.generateCode(6)) + ".png";
        int dirLastIndex = Global.getProfile().length() + 1;
        String currentDir = StringUtils.substring(UploadConstants.ModelEnum.CREDIT_PRODUCT_EXCHANGE_QR_CODE.getUploadPath(), dirLastIndex);
        String filePath = Constants.RESOURCE_PREFIX + "/" + currentDir + fileName;
//        CloudStorageService cloudStorageService = OSSFactory.build();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "words=" + url);
        Request request = new Request.Builder()
                .url(QR_CODE_URL)
                .method("POST", body)
                .addHeader("X-APISpace-Token", TOKEN)
                .addHeader("Authorization-Type", "apikey")
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        try {
            Response response = client.newCall(request).execute();
            if (response.body() != null) {
                String result = response.body().string();
                JSONObject jsonObject = JSON.parseObject(result);
                Integer resultCode = jsonObject.getInteger("code");
                if (200 == resultCode) {
                    return jsonObject.getJSONObject("data").getString("qr_path");
                }
            }
//            return cloudStorageService.upload(inputStream, "jyz" + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static void sendSmsNotify(String content) {
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, content);
        Request request = new Request.Builder()
                .url(SMS_NOTIFY_URL)
                .method("POST", body)
                .addHeader("X-APISpace-Token", TOKEN)
                .addHeader("Authorization-Type", "apikey")
                .addHeader("Content-Type", "application/json")
                .build();
        try {
            client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
